import {LogLanguage} from "../dist/index.js"

// 测试参数类型和SQL语句识别
const testCases = [
  // 参数类型测试
  "2024-01-15 10:30:45 [INFO] Parameters: points_package_id(String) user_id(Long) is_active(Boolean)",
  
  // SQL语句测试
  "2024-01-15 10:30:45 [INFO] Preparing: UPDATE users SET status = ? WHERE id = ?",
  
  // 混合测试
  "2024-01-15 10:30:45 [INFO] [TID:] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] Parameters: user_id(Integer) name(String) age(Integer)",
  
  // 复杂SQL测试
  "2024-01-15 10:30:45 [INFO] Executing: SELECT * FROM users WHERE status = ? AND age > ?",
  
  // 多个SQL语句
  "2024-01-15 10:30:45 [INFO] CREATE TABLE users (id INTEGER, name VARCHAR(255))",
  
  // 参数类型和SQL混合
  "2024-01-15 10:30:45 [INFO] INSERT INTO logs (message_id(String), level(Integer), timestamp(Long)) VALUES (?, ?, ?)"
]

console.log("=== 参数类型和SQL语句识别测试 ===")

testCases.forEach((testCase, index) => {
  console.log(`\n测试 ${index + 1}:`)
  console.log(`输入: ${testCase}`)
  
  const tree = LogLanguage.parser.parse(testCase)
  let foundNodes = []
  
  tree.iterate({
    enter: (node) => {
      foundNodes.push({
        name: node.type.name,
        from: node.from,
        to: node.to,
        text: testCase.slice(node.from, node.to)
      })
    }
  })
  
  console.log("识别的节点:")
  foundNodes.forEach(node => {
    console.log(`  - ${node.name}: "${node.text}" (${node.from}-${node.to})`)
  })
  
  // 检查参数类型
  const paramTypeRegex = /\b([a-zA-Z_]+)\s*\(([A-Z][a-zA-Z]*)\)/g
  let match
  let foundParamTypes = []
  
  while ((match = paramTypeRegex.exec(testCase)) !== null) {
    foundParamTypes.push({
      full: match[0],
      param: match[1],
      type: match[2]
    })
  }
  
  if (foundParamTypes.length > 0) {
    console.log("\n✓ 参数类型识别成功:")
    foundParamTypes.forEach(param => {
      console.log(`  - "${param.full}" (参数: ${param.param}, 类型: ${param.type})`)
    })
  } else {
    console.log("\n✗ 未找到参数类型")
  }
  
  // 检查SQL语句
  const sqlRegex = /\b(SELECT|UPDATE|INSERT|DELETE|CREATE|DROP|ALTER)\b/gi
  let foundSQLStatements = []
  
  while ((match = sqlRegex.exec(testCase)) !== null) {
    foundSQLStatements.push({
      keyword: match[1],
      index: match.index
    })
  }
  
  if (foundSQLStatements.length > 0) {
    console.log("✓ SQL语句识别成功:")
    foundSQLStatements.forEach(sql => {
      console.log(`  - "${sql.keyword}" (位置: ${sql.index})`)
    })
  } else {
    console.log("✗ 未找到SQL语句")
  }
  
  // 检查其他功能
  const dateNodes = foundNodes.filter(node => node.name === 'DATEExpression')
  const logLevelNodes = foundNodes.filter(node => 
    node.name.includes('Expression') && 
    (node.name.includes('FATAL') || node.name.includes('ERROR') || 
     node.name.includes('INFO') || node.name.includes('WARNING') || 
     node.name.includes('Debug'))
  )
  const tidNodes = foundNodes.filter(node => 
    node.name.includes('TID') || 
    node.text.includes('fcdfba1a21214a8e97c7d8fd1a8aa287')
  )
  
  console.log("\n其他功能检查:")
  console.log(`  - 日期识别: ${dateNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 日志级别识别: ${logLevelNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - TID识别: ${tidNodes.length > 0 ? '✓' : '✗'}`)
})
